package com.louis.epay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;

import com.louis.epay.util.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.UUID;

@RestController
@RequestMapping("/alpay")
public class Alipay {

    /**
     * 说明：单笔转账到支付宝账户
     * out_biz_no  商户转账唯一订单号
     * payee_type 收款方账户类型  (1、ALIPAY_USERID：支付宝账号对应的支付宝唯一用户号。以2088开头的16位纯数字组成。2、ALIPAY_LOGONID：支付宝登录号，支持邮箱和手机号格式。)
     *
     * @param payee_account 收款方账户
     * @param amount        转账金额
     *                      payer_show_name 付款方姓名
     *                      payee_real_name 收款方真实姓名
     *                      remark 转账备注
     *                      String AuthCode
     */
    @RequestMapping("/kabi")
    public void transferAccounts(String payee_account, String amount, String remark) {

        //填写自己创建的app的对应参数
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2016092900621689",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCFiOovByzPH4riJu2E1oXuAeLhy49nUWRzmKdR0BTRFBW/RUCv9alXO6w4vyjTrOwBWAFMOSKpdcE25FJpZ+nVnDfvlCDl/paA9im/jCobNQvY8UcNMcN3Wd6JsORwHrbbrM1SFRplbzWDUPtqDCU92DmOu1D25m0q9LJtFBaHTkBCDR/GOKPLmIlVwBVDK/5JR8B5Jko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ctlUjDYVQUm4YZB4pMkrLT3PGktDPVCYjspVOHgFMYM0u04KJ4mBtCMdLCUyrlUsXSX6JA1XKSvIz6LgGSVp2lhVq+KGeRKK5mtS9+Ht31qidGLxSUQ5N0AJAoGBAIsBLSDdKH/Auy3eeOiXSh3IBxXTzabPe0d3jeeV6KuYY7bQLoSiNBY3sQTyDyLNyX+xcqjBbpfjgAXKeporU0DAl6aA/+/LqR/ildq0qXguZBzJ3L93CmHMQ/C5czCxFo8gX2QN4YgYd5ALhLFtb62mdnfXrDsnO3B7dnDZLYhK",
                "json",
                "utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAskqNnzJXrmHrHXcKKkGjvViYKOnvpF56PrRXHEM1EsFECCDwPEAgbeRl8rt+VzLMPHHVAGbp0awhCN3rSxsMbUPgVNVjrQisPybRAL36tVi5CTdqWsQL37v0JRacDinvCqte4apfPFLixU+JWF2zfNTGjC5+g0pfQzCNG+G1PzWQLZb3kLXo4CeJJUA2e3aARsdW4rqcDCF3ly/LravaM0wd75G9KJ6VGTm4AiYfxazYyZz5PFmGegzmrfDGroSTY0wnFqFVWsffCjyp9NhmReUsoFglRz7vyWnsgyeIRK0UJn716nmzaYxNofi6mBfnwIMD7oplhKnf0iQxY2DJKwIDAQAB",
                "RSA2");


        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();


        AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
        model.setOutBizNo(DateUtil.getCurrent());
        model.setPayeeType("ALIPAY_LOGONID");
        model.setPayeeAccount(payee_account);
        model.setAmount(amount);
/*        model.setPayerShowName("xiaorui");
        model.setPayeeRealName(payee_real_name);
        model.setRemark("回收");*/
        request.setBizModel(model);
        try {
            AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
            System.out.println(response.toString());

            if ("10000".equals(response.getCode())) {
                System.out.println("转账成功");
            } else {
                System.out.println("转账失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }


    }


}